package com.student.user.controller;

import com.student.user.pojo.Admin;
import com.student.user.service.AdminService;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import util.JwtUtil;

import java.util.HashMap;
import java.util.Map;

@Controller
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private JwtUtil jwtUtil;

    public ModelAndView login(Admin admin) {

        Admin adminLogin = adminService.login(admin);

        ModelAndView mv = new ModelAndView();

        if (adminLogin == null) {
            mv.addObject("status", StatusCode.LOGINERROR);
        } else {
            String token = jwtUtil.createJWT(adminLogin.getId(), adminLogin.getName(), "admin");
            mv.addObject("status", StatusCode.OK);
            mv.addObject("token", token);
            // 填写登录成功后出现页面
            mv.setViewName("");
        }

        return mv;
    }

}


